/**
 * Edge Functions 模块 API
 * 边缘函数相关接口
 */

import { request, type ApiResponse, type RequestParamValue } from "../request";

// ============ 类型定义 ============

/**
 * 函数状态
 */
export type FunctionStatus = "ACTIVE" | "REMOVED" | "THROTTLED";

/**
 * 函数信息
 */
export interface EdgeFunction {
	id: string;
	slug: string;
	name: string;
	status: FunctionStatus;
	version: number;
	created_at: number;
	updated_at: number;
	verify_jwt?: boolean;
	import_map?: boolean;
	entrypoint_path?: string;
	import_map_path?: string;
	ezbr_sha256?: string;
}

/**
 * 创建函数请求
 */
export interface CreateFunctionBody {
	slug: string;
	name: string;
	body: string;
	verify_jwt?: boolean;
}

/**
 * 更新函数请求
 */
export interface UpdateFunctionBody {
	name?: string;
	body?: string;
	verify_jwt?: boolean;
}

/**
 * 批量更新函数项
 */
export interface BulkUpdateFunctionItem {
	id: string;
	slug: string;
	name: string;
	status: FunctionStatus;
	version: number;
	created_at?: number;
	verify_jwt?: boolean;
	import_map?: boolean;
	entrypoint_path?: string;
	import_map_path?: string;
	ezbr_sha256?: string;
}

/**
 * 批量更新函数响应
 */
export interface BulkUpdateFunctionResponse {
	functions: EdgeFunction[];
}

/**
 * 部署函数响应
 */
export interface DeployFunctionResponse {
	id: string;
	slug: string;
	name: string;
	status: FunctionStatus;
	version: number;
	created_at?: number;
	updated_at?: number;
	verify_jwt?: boolean;
	import_map?: boolean;
	entrypoint_path?: string;
	import_map_path?: string;
	ezbr_sha256?: string;
}

// ============ API 方法 ============

/**
 * 获取所有边缘函数
 */
export function listFunctions(ref: string): Promise<ApiResponse<EdgeFunction[]>> {
	return request.get<EdgeFunction[]>(`/v1/projects/${ref}/functions`);
}

/**
 * 创建边缘函数（已废弃，推荐使用 deployFunction）
 */
export function createFunction(
	ref: string,
	data: CreateFunctionBody,
	params?: {
		slug?: string;
		name?: string;
		verify_jwt?: boolean;
		import_map?: boolean;
		entrypoint_path?: string;
		import_map_path?: string;
		ezbr_sha256?: string;
	}
): Promise<ApiResponse<EdgeFunction>> {
	return request.post<EdgeFunction>(
		`/v1/projects/${ref}/functions`,
		data,
		{ params: params as Record<string, RequestParamValue> }
	);
}

/**
 * 批量更新函数
 */
export function bulkUpdateFunctions(
	ref: string,
	data: BulkUpdateFunctionItem[]
): Promise<ApiResponse<BulkUpdateFunctionResponse>> {
	return request.put<BulkUpdateFunctionResponse>(
		`/v1/projects/${ref}/functions`,
		data
	);
}

/**
 * 部署函数
 * 注意：此接口需要特殊的 multipart/form-data 处理
 */
export function deployFunction(
	ref: string,
	params?: {
		slug?: string;
		bundleOnly?: boolean;
	}
): Promise<ApiResponse<DeployFunctionResponse>> {
	return request.post<DeployFunctionResponse>(
		`/v1/projects/${ref}/functions/deploy`,
		undefined,
		{ params: params as Record<string, RequestParamValue> }
	);
}

/**
 * 获取指定函数
 */
export function getFunction(
	ref: string,
	functionSlug: string
): Promise<ApiResponse<EdgeFunction>> {
	return request.get<EdgeFunction>(
		`/v1/projects/${ref}/functions/${functionSlug}`
	);
}

/**
 * 更新函数
 */
export function updateFunction(
	ref: string,
	functionSlug: string,
	data: UpdateFunctionBody,
	params?: {
		slug?: string;
		name?: string;
		verify_jwt?: boolean;
		import_map?: boolean;
		entrypoint_path?: string;
		import_map_path?: string;
		ezbr_sha256?: string;
	}
): Promise<ApiResponse<EdgeFunction>> {
	return request.patch<EdgeFunction>(
		`/v1/projects/${ref}/functions/${functionSlug}`,
		data,
		{ params: params as Record<string, RequestParamValue> }
	);
}

/**
 * 删除函数
 */
export function deleteFunction(
	ref: string,
	functionSlug: string
): Promise<ApiResponse<Record<string, never>>> {
	return request.delete<Record<string, never>>(
		`/v1/projects/${ref}/functions/${functionSlug}`
	);
}

/**
 * 获取函数代码
 */
export function getFunctionBody(
	ref: string,
	functionSlug: string
): Promise<ApiResponse<Blob>> {
	return request.get<Blob>(
		`/v1/projects/${ref}/functions/${functionSlug}/body`
	);
}

export default {
	listFunctions,
	createFunction,
	bulkUpdateFunctions,
	deployFunction,
	getFunction,
	updateFunction,
	deleteFunction,
	getFunctionBody
};
